import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes, bannerList, gooditem, HotGoods, Brand, HomeProduct, Special } from '@/types/data'
export default defineStore('home', {
  state() {
    return {
      //轮播图广告数据
      bannerList: [] as bannerList[],
      // 新鲜好物的数据
      newGoodList: [] as gooditem[],
      //人气推荐数据
      hotGoodList: [] as HotGoods[],
      // 品牌数据
      brandList: [] as Brand[],
      //商品推荐数据
      productList: [] as HomeProduct[],
      //最新专题数据
      specialList: [] as Special[],
    }
  },
  actions: {
    //获取轮播广告数据
    async getBannerList() {
      const res = await request.get<ApiRes<bannerList>>('/home/banner')
      // console.log(res.data.result);
      this.bannerList = res.data.result
    },
    // 获取新鲜好物数据
    async getnewGoodList() {
      const res = await request.get<ApiRes<gooditem>>('/home/new')
      // console.log(res.data.result);
      this.newGoodList = res.data.result
    },
    //获取人气推荐数据
    async gethotList() {
      const res = await request.get<ApiRes<HotGoods>>('/home/hot')
      // console.log(res.data.result);
      this.hotGoodList = res.data.result
    },
    // 获取品牌数据
    getBrandList() {
      setTimeout(async () => {
        const res = await request.get<ApiRes<Brand>>('/home/brand')
        // console.log(res.data.result);
        this.brandList = res.data.result
      }, 1000);
    },
    //获取商品推荐数据
    async getProductList() {
      const res = await request.get<ApiRes<HomeProduct>>('/home/goods')
      this.productList = res.data.result
    },
    //获取最新专题数据
    async getSpecialList() {
      const res = await request.get<ApiRes<Special>>('/home/special')
      this.specialList = res.data.result
    },
  }
})